#include<stdio.h>
#include<malloc.h>

typedef struct {int* data;int capacity,top; } stack;

stack* stack_new(int capacity){
    stack* s = malloc(sizeof(stack));
    s->capacity = capacity;
    s->top = 0;
    s->data = malloc(sizeof(int)*capacity);
    return s;
}

void stack_del(stack* s){free(s->data);free(s);}

int stack_pop(stack* s){
    if(s->top==0){perror("Empty stack");return 0;}
    return s->data[--s->top];
}

void stack_push(stack* s,int data){if(s->top < s->capacity)s->data[s->top++] = data;}

int stack_peek(const stack* s){
    if(s->top==0){perror("\nStack Empty Error.\n"); return 0; }
    return s->data[s->top-1];
}

int isp(int tok){switch(tok){
    case '(':return 0;
    case '+':case '-':return 1;
    case '*':case '/':return 2;
    case '^':return 3;
    default: perror("\nEncountered illegal Operator.\n");return -1;
}}

int icp(int tok){switch(tok){
    case '+':case '-':return 1;
    case '*':case '/':return 2;
    case '^':return 4;
    case '(':return 5;
    default: perror("\nEncountered illegal Operator.\n");return -1;
}}


int main(){
    char buf;
    stack* s = stack_new(1024);
    while( '#' != (buf = getchar() )  ){
        switch(buf){
            // GCC support
            case '0'...'9':putchar(buf);break;
            case ')':putchar(' ');
                while( ( '(' != ( buf = stack_pop(s))  ) && s->top )
                    putchar(buf);
                if(buf!='(' && s->top){
                    perror("\nEncountered illegal expression.\n");
                    return -1;
                }break;
            case '+':case '-':case '*':case '/':case '^':putchar(' ');case '(':
                while( ( s->top ? isp( stack_peek(s) ) : 0 ) >= icp(buf)  )
                    putchar( stack_pop(s) );
                stack_push(s,buf);
        }
    }
    while(s->top)putchar(stack_pop(s));
    stack_del(s);
    return 0;
}